<template>
  <div>
    <!-- 顶部导航栏 -->
    <!-- <topBar v-if="$route.meta.topBar != false"></topBar> -->
    <!--end 顶部导航栏 -->
    <router-view />
    <!-- 底部导航栏 -->
    <bottomTabs v-if="$route.meta.bottomTabs == true&& $store.state.userList.role !='B'"></bottomTabs>
    <!--end 底部导航栏 -->
  </div>
</template>
<script>
import bottomTabs from "@/components/global/bottomTabs";
import topBar from "@/components/global/topBar";
import { realconsole } from "@/assets/js/buriedPoint";
export default {
  name: "App",
  data() {
    return {
      homePath: "/home", //production 浙里办首页地址 /
      loginPath: "/", //production 浙里办登录地址 /
      // homePath: process.env.NODE_ENV =='production'? "/":'/home',//production 浙里办首页地址 /
      // loginPath: process.env.NODE_ENV =='production'? "/home":'/',//production 浙里办登录地址 /
    };
  },
  mounted() {
    console.log("versions", this.$store.state.versions);
    return
    this.$router.onReady(() => {
      var role = this.$route.query.role;
      if (role) {
        if (role == "B") {
          this.homePath = "/FinancialService/index";
        } else if (role == "L") {
          this.homePath = "/NonPublicOwnershipRights/legalService_consult";
        }
      }
      console.log("appToken", this.$route, this.$route.query.appToken);
      // window.localStorage.clear(); //1.0.30 登录逻辑改变 清除用户token
      // 存储当前环境
      var userList = window.localStorage.userList;
      if (userList && JSON.parse(userList).appToken) {
        //缓存已有登录信息
        if (this.$route.path != this.loginPath) {
          //非登录页调获取用户信息接口
          this.userInfo_index();
        } else {
          //登录页免登
          //校验当前登录页面端类型和缓存类型是否匹配
          if (role && role == JSON.parse(userList).role) {
            this.$router.replace(this.homePath);
          } else if (JSON.parse(userList).role == "C") {
            //没有传role表示企业端
            this.$router.replace(this.homePath);
          }
        }
      } else {
        //缓存没有信息
        if (this.$route.query.appToken) {
          // 带token跳转过来 单点登录
          this.$store.state.token = this.$route.query.appToken;
          this.userInfo_index(true);
        } else {
          //没带token
          console.log("this.$route.path", this.$route.path);
          if (process.env.NODE_ENV == "production") {
            window.location.href =
              "https://api.xiangcun0577.com/ato/zjzwfw/sso/login?mobile=true";
            // this.getUserForDandian(); //根据能不能获取到用户类型 来判断是否是浙里办打开（第一次登录的用户获取不到用户信息 暂时废弃）
          } else {
            //普通版本 不单点登录
          }
        }
      }
      if (process.env.NODE_ENV === "production") {
        //非开发环境
        ZWJSBridge.onReady(() => {
          console.log("初始化完成后，执行bridge方法");
          this.getUiStyle(); //获取用户当前UI风格
          this.getUser(); //浙里办获取用户类型和用户经纬度
        });
      }
      this.howApp();
    });
  },
  watch: {
    $route(to, from) {
      // console.log(to);
      if (process.env.NODE_ENV !== "development") {
        realconsole(to.path, to.name, location.href); //埋点
      }
    },
  },
  components: {
    bottomTabs,
    topBar,
  },
  methods: {
    //支付宝 浙里办判断单点跳转
    howAppDandian() {
      // 浙里办支付宝小程序判断
      const sUserAgent = window.navigator.userAgent.toLowerCase();
      const bIsDtDreamApp = sUserAgent.indexOf("dtdreamweb") > -1; // 浙里办APP
      const bIsAlipayMini =
        sUserAgent.indexOf("miniprogram") > -1 &&
        sUserAgent.indexOf("alipay") > -1;

      console.log("app", sUserAgent, bIsAlipayMini);

      if (bIsDtDreamApp) {
        console.log("浙里办APP打开");
        //浙里办版本(生产环境) 直接免登
        //跳转浙里办登录 实现单点登录
        var appUrl =
          "https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2001895675/reserved/index.html#/debug=true";
        // window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=psyljyylfwpt&goto=${appUrl}`;
        window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=psyljyylfwpt`;
      }
      if (bIsAlipayMini) {
        //浙里办版本(生产环境) 直接免登
        //跳转浙里办登录 实现单点登录
        console.log("浙里办支付宝小程序");
        var appUrl =
          "https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2001895675/reserved/index.html#/debug=true";
        window.location.href = `https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=psyljyylfwpt`;
      }
    },
    howApp() {
      var that = this;
      const sUserAgent = window.navigator.userAgent.toLowerCase();
      const bIsDtDreamApp = sUserAgent.indexOf("dtdreamweb") > -1; // 浙里办APP
      const bIsAlipayMini =
        sUserAgent.indexOf("miniprogram") > -1 &&
        sUserAgent.indexOf("alipay") > -1;
      console.log("step1");
      if (bIsDtDreamApp) {
        that.watchApp();
      } else if (bIsAlipayMini) {
        // that.watchApply();
      }
    },
    watchApp() {
      console.log("step2");
      var that = this;
      window.addEventListener(
        "pageshow",
        function (event) {
          if (
            event.persisted ||
            (window.performance && window.performance.navigation.type == 2)
          ) {
            ZWJSBridge.close() //这个是浙里办内部的api，调用close()接口关闭通过openLink打开的页面
              .then((result) => {
                console.log(result);
              })
              .catch((error) => {
                console.log(error);
              });
          }
          // that.isLoad();
        },
        false
      );
    },
    watchApply() {
      console.log("step3");
      var that = this;
      window.addEventListener(
        "pageshow",
        function (event) {
          if (
            event.persisted ||
            (window.performance &&
              (window.performance.navigation.type == 1 ||
                window.performance.navigation.type == 0))
          ) {
            that.isLoad();
          } else if (
            event.persisted ||
            (window.performance && window.performance.navigation.type == 2)
          ) {
            my.navigateBack();
          } else {
            my.navigateBack(); //支付宝小程序的api，需在index.html引入，通过此api直接跳回浙里办小程序页面
          }
        },
        false
      );
    },
    getUserForDandian() {
      //根据能不能获取到用户类型 来判断是否是浙里办打开
      ZWJSBridge.getUserType()
        .then((result) => {
          console.log("login1", result);
          //跳转浙里办登录 实现单点登录
          // 				window.location.href =
          // 'https://api.xiangcun0577.com/ato/zjzwfw/sso/login?mobile=true';
          window.location.replace(
            "https://api.xiangcun0577.com/ato/zjzwfw/sso/login?mobile=true"
          );
        })
        .catch((error) => {
          console.log("login2", error);
        });
    },
    getUiStyle() {
      //获取用户当前UI风格
      // 			 normal：常规版
      // elder：老年版
      console.log("进入getUiStyle", ZWJSBridge.getUiStyle);
      ZWJSBridge.getUiStyle()
        .then((result) => {
          console.log("获取用户当前UI风格", result);
          if (result.uiStyle == "elder") {
            $store.commit("SET_oldPage", true);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getUser() {
      //浙里办获取用户类型和用户经纬度
      console.log("进入getUser", ZWJSBridge.getLocation);
      ZWJSBridge.getLocation()
        .then((result) => {
          console.log("获取用户类型和用户经纬度", result);
          window.localStorage.setItem(
            "appUser",
            JSON.stringify({
              long: result.longitude,
              lati: result.latitude,
            })
          );
          console.log("已存缓存用户数据", window.localStorage.appUser);
        })
        .catch((error) => {
          console.log(error);
        });
      ZWJSBridge.getUserType()
        .then((result) => {
          console.log(result);
          window.localStorage.setItem("userType", result.userType);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    userInfo_index(goHome) {
      var params = {};
      this.$myApi.userInfo_index(params).then((res) => {
        if (res.code == 200) {
          if (this.$store.state.token) {
            res.data.appToken = this.$store.state.token;
          }
          this.$store.state.userList = res.data;
          window.localStorage.setItem("userList", JSON.stringify(res.data));
          if (process.env.NODE_ENV !== "development") {
            //非开发环境
            realconsole(this.$route.path, this.$route.name, location.href); //埋点
          }
          if (goHome) {
            this.$router.replace(this.homePath);
          }
        } else {
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
@import url("~@/assets/css/common.css");
@import url("~@/assets/css/variable.less");
@import url("~@/assets/css/components/tableContaner.less");
@import url("~@/assets/css/components/dialog.less");
</style>